package com.ghsc.ghxc.jsb.ui.message.activity

import android.os.Bundle
import android.text.TextUtils
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.getCompatColor
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GsonUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ActivityCreateGroupNameTypeBinding
import com.ghsc.ghxc.jsb.ui.message.bean.GroupTypeItem
import com.ghsc.ghxc.jsb.ui.message.bean.GroupTypeList
import com.ghsc.ghxc.jsb.ui.message.view.GroupTypeListPopupView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.message.activity
 * 类名：CreateGroupNameTypeActivity
 * 创建时间：2024/5/9 22:46
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class CreateGroupNameTypeActivity :
    BaseVMActivity<CreateGroupNameTypeViewModel, ActivityCreateGroupNameTypeBinding>(){
    override fun attachLayoutRes(): Int {
        return R.layout.activity_create_group_name_type
    }

    override fun attachVMClass(): Class<CreateGroupNameTypeViewModel>? {
        return CreateGroupNameTypeViewModel::class.java
    }

    var mListTypes: MutableList<GroupTypeItem> = ArrayList()
    var itemBen :GroupTypeItem ?= null

    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }
    override fun initView() {
        binding.run {
            bar.topTitle.text="创建群聊"
            bar.topLeftView.setSingleClickListener {
                hideKeyboard(etName)
                finish()
            }
        }
        val configBean= UserManager.getConfig()
        configBean?.let {
//            GroupTypeList
            var sysConfigResultData = it.getSysConfigBean(CustKey.configK52 )
            if(sysConfigResultData!=null)
                if(!TextUtils.isEmpty(sysConfigResultData.cfg_value)) {
                    var groupTypeList =
                        GsonUtil.GsonToJsonClass(
                            sysConfigResultData.cfg_value,
                            GroupTypeList::class.java
                        )
                    if (groupTypeList.size>0){
                        for (i in groupTypeList.indices){
                            loge("xxx="+groupTypeList[i].toString())
                            if (!groupTypeList[i].is_official)
                                mListTypes.add(groupTypeList[i])
                        }
                        itemBen= mListTypes[0]

                        binding.tvType.text=mListTypes[0].title
                    }
                }

        }
    }
    override fun initClickListener() {
        super.initClickListener()
        binding.run {
            llBtnType.setSingleClickListener {
                //选择类型
                if (mListTypes.size>0)
                    for ( i in mListTypes.indices)
                        mListTypes[i].isSelect = mListTypes[i].classify_id ==itemBen!!.classify_id
                XPopup.Builder(this@CreateGroupNameTypeActivity)
                    .popupAnimation(PopupAnimation.ScaleAlphaFromCenter  )
                    .customHostLifecycle(lifecycle)
                    .dismissOnTouchOutside(true)
                    .navigationBarColor(getCompatColor(R.color.bg_dark_color))
                    .asCustom(
                        GroupTypeListPopupView(this@CreateGroupNameTypeActivity,
                            mListTypes,
                            tvBtnSub = {
                                loge("选择的是：${it.title}")
                                itemBen= it
                                tvType.text= itemBen!!.title
                            }
                        )
                    )
                    .show()
            }
            tvNext.setSingleClickListener {
                if (TextUtils.isEmpty(etName.text.toString().trim())){
                    showToast("请输入群昵称")
                    return@setSingleClickListener
                }
                var  bundle = Bundle()
                bundle.putString("name",etName.text.toString().trim())
                bundle.putInt("classify_id",itemBen!!.classify_id)
                startActivityFinish(ChatListActivity().javaClass,bundle)
            }
        }
    }
    }